Import Python modules:

  • Shapefile to read shp files
  • pandas to read mobility data
  • numpy for array manipulation
  • bokeh for interactive plotting
Loading BokehJS ...

Read in United Kingdom shapefile (second level subdivisions) downloaded from [GADM].(https://gadm.org/download_country_v3.html)

shapefile Reader
    183 shapes (type 'POLYGON')
    183 records (14 fields)

Records file contains the name of the corresponding region shape and additional labels.

Out[3]:
Record #0: ['GBR', 'United Kingdom', 'GBR.1_1', 'England', '', 'GBR.1.1_1', 'Barnsley', '', '', 'Metropolitan Borough', 'Metropolitan Borough', '', 'GB.BX']

Convert the shapes and records into (x,y)-coordinates to be plotted as patches using bokeh.

  • region_xcoords: x-coords for each region boundary
  • region_ycoords: y-coords for each region boundary
  • region_names: names for each region boundary
  • region_codes: Area_Codes for each (sub)region boundary

Import the Global Mobility Data, and restrict to the UK data.

/home/ollie/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3058: DtypeWarning: Columns (3) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)

Rename and group regions to correspond with the naming and grouping between the 2 data sets. For example we group the shapefile regions Barnsley, Doncaster, Rotherham and Sheffield to the South Yorkshire mobility data entry.

Let's arbitrarily choose to visualise the grocery_and_pharmacy_percent_change_from_baseline data.

Out[7]:
country_region_code country_region sub_region_1 sub_region_2 iso_3166_2_code census_fips_code date retail_and_recreation_percent_change_from_baseline grocery_and_pharmacy_percent_change_from_baseline parks_percent_change_from_baseline transit_stations_percent_change_from_baseline workplaces_percent_change_from_baseline residential_percent_change_from_baseline
74231 GB United Kingdom NaN NaN NaN NaN 2020-02-15 -12.0 -7.0 -35.0 -12.0 -4.0 2.0
74232 GB United Kingdom NaN NaN NaN NaN 2020-02-16 -7.0 -6.0 -28.0 -7.0 -3.0 1.0
74233 GB United Kingdom NaN NaN NaN NaN 2020-02-17 10.0 1.0 24.0 -2.0 -14.0 2.0
74234 GB United Kingdom NaN NaN NaN NaN 2020-02-18 7.0 -1.0 20.0 -3.0 -14.0 2.0
74235 GB United Kingdom NaN NaN NaN NaN 2020-02-19 6.0 -2.0 8.0 -4.0 -14.0 3.0

We load in the grocery and pharmacy data replacing missing data with the string 'null'.

Initialise the dates over which we plot our data.

We will plot a UK map showing the percentage change from baseline of grocery and pharmacy travel. For more details about the data see Community Mobility Reports.

We will provide a date slider to interact with the plot to see how this varies thorughout the lockdown period. Tapping on a region will plot the percentage change from baseline of that region as a line graph.

Define ColumnDataSources for the plots.

We use javascript rather than python callbacks so we can host the interactive map on github pages.

We observe the general trend that the use of grocery and pharmacy shops increases above the baseline rate around $\sim$18 March before reducing significantly during lockdown.

There are a number of further developments to add to this tool:

  • Optimise performance
    • Decrease level of detail in plot whilst interacting
    • Use alternative data structures for more efficient updates
  • Add dropdown menu to switch between each data set
  • Overlay travel information on map